import { ajax, toast } from 'nc-lightapp-front';

export default function (btnEventConfig, afterClick, defCtrlEditTableArea, buttonGroupprops, buttonEvent, beforeResult) {
    let statusVar = window.presetVar.status;

    const { ncProps } = window._ssc_gy_profile;

    let ctrlEditTableArea = btnEventConfig.ctrlEditTableArea || defCtrlEditTableArea || [];
    let ctrlFormArea = buttonGroupprops.ctrlFormArea || [];

    let saveUrl = btnEventConfig.url;

    let filterEmptyRows = btnEventConfig.filterEmptyRows;
    if (filterEmptyRows) {
        filterEmptyRows = Object.values(filterEmptyRows);
    }

    let getSendParam = btnEventConfig.getSendParam;

    let reqData = {
        data: {}
    }


    //props.validateToSave( data , callback , tableTypeObj , billType )

    let saveData = {};

    let handle = () => {
        ajax({
            url: saveUrl,
            data: saveData,
            success: (data) => {
                if (btnEventConfig.showSuccessMsg != false) {
                    // "105601GGZD-0067": "保存成功！",
                    toast({ title: this.state.multiLang['105601GGZD-0067'] });
                }
                ctrlEditTableArea.forEach((areaId) => {
                    let buttonsVisible = {};
                    ncProps.button.getButtons().map((btnOne, index) => {
                        if (this.props.area == btnOne.area) {
                            if (btnOne.children) {
                                btnOne.children.map((btnOneChild, index) => {
                                    if (this.props.area == btnOneChild.area) {
                                        // if(btnOne.key == 'Add' || ((buttonEvent || {})[btnOne.key] || {}).ActionCode == 'Add'){
                                        //     buttonsVisible[btnOne.key]=true;
                                        // }else 
                                        if (btnOneChild.key == 'Edit' || ((buttonEvent || {})[btnOneChild.key] || {}).ActionCode == 'Edit') {
                                            buttonsVisible[btnOneChild.key] = true;
                                        } else if (btnOneChild.key == 'Save' || ((buttonEvent || {})[btnOneChild.key] || {}).ActionCode == 'Save') {
                                            buttonsVisible[btnOneChild.key] = false;
                                        } else if (btnOneChild.key == 'Cancel' || ((buttonEvent || {})[btnOneChild.key] || {}).ActionCode == 'Cancel') {
                                            buttonsVisible[btnOneChild.key] = false;
                                        }
                                    }
                                });
                            }
                            if (btnOne.key == 'Edit' || ((buttonEvent || {})[btnOne.key] || {}).ActionCode == 'Edit') {
                                buttonsVisible[btnOne.key] = true;
                            } else if (btnOne.key == 'Save' || ((buttonEvent || {})[btnOne.key] || {}).ActionCode == 'Save') {
                                buttonsVisible[btnOne.key] = false;
                            } else if (btnOne.key == 'Cancel' || ((buttonEvent || {})[btnOne.key] || {}).ActionCode == 'Cancel') {
                                buttonsVisible[btnOne.key] = false;
                            }
                        }
                    })
                    ncProps.button.setButtonsVisible(buttonsVisible);

                    ncProps.editTable.setStatus(areaId, 'browse');
                    data && data.data && data.data[areaId] && ncProps.editTable.setTableData(areaId, data.data[areaId]);
                    // 处理后事件
                    afterClick && afterClick(ncProps, data);

                })
                ctrlFormArea.forEach((areaId) => {
                    let buttonsVisible = {};
                    ncProps.button.getButtons().map((btnOne, index) => {
                        if (this.props.area == btnOne.area) {
                            if (btnOne.children) {
                                btnOne.children.map((btnOneChild, index) => {
                                    if (this.props.area == btnOneChild.area) {
                                        // if(btnOne.key == 'Add' || ((buttonEvent || {})[btnOne.key] || {}).ActionCode == 'Add'){
                                        //     buttonsVisible[btnOne.key]=true;
                                        // }else 
                                        if (btnOneChild.key == 'Edit' || ((buttonEvent || {})[btnOneChild.key] || {}).ActionCode == 'Edit') {
                                            buttonsVisible[btnOneChild.key] = true;
                                        } else if (btnOneChild.key == 'Save' || ((buttonEvent || {})[btnOneChild.key] || {}).ActionCode == 'Save') {
                                            buttonsVisible[btnOneChild.key] = false;
                                        } else if (btnOneChild.key == 'Cancel' || ((buttonEvent || {})[btnOneChild.key] || {}).ActionCode == 'Cancel') {
                                            buttonsVisible[btnOneChild.key] = false;
                                        }
                                    }
                                });
                            }
                            if (btnOne.key == 'Edit' || ((buttonEvent || {})[btnOne.key] || {}).ActionCode == 'Edit') {
                                buttonsVisible[btnOne.key] = true;
                            } else if (btnOne.key == 'Save' || ((buttonEvent || {})[btnOne.key] || {}).ActionCode == 'Save') {
                                buttonsVisible[btnOne.key] = false;
                            } else if (btnOne.key == 'Cancel' || ((buttonEvent || {})[btnOne.key] || {}).ActionCode == 'Cancel') {
                                buttonsVisible[btnOne.key] = false;
                            }
                            // else if(btnOne.key == 'Add' || ((buttonEvent || {})[btnOne.key] || {}).ActionCode == 'Add'){
                            //     buttonsVisible[btnOne.key]=false;
                            // } 
                        }
                    })
                    ncProps.button.setButtonsVisible(buttonsVisible);
                    ncProps.form.setFormStatus(areaId, 'browse');
                    data && data.data && data.data[areaId] && ncProps.form.setAllFormValue({ [areaId]: data.data[areaId] });
                    // 处理后事件
                    afterClick && afterClick(ncProps, data);

                })
            }
        })
    }

    if (saveUrl != null && ctrlEditTableArea.length != 0) {
        let flag = true;
        ctrlEditTableArea.forEach((areaId) => {
            flag = ncProps.editTable.checkRequired(areaId, []);
            if (filterEmptyRows) {
                ncProps.editTable.filterEmptyRows(...filterEmptyRows)
            }
            saveData[areaId] = ncProps.editTable.getAllData(areaId);
        })
        ctrlFormArea.forEach((areaId) => {
            saveData[areaId] = ncProps.form.getAllFormValue(areaId);
        })

        if (getSendParam != null) {
            saveData = btnEventConfig.getSendParam(saveData);
        }
        if (flag) {
            if (beforeResult.extReqParams) {
                saveData = { ...beforeResult.extReqParams, ...saveData }
            }

            //处理保存公式，目前只处理存table形式的
            if (!ctrlFormArea.length) {
                
                let dataForFormulate = {grid : {}}
                ctrlEditTableArea.forEach((areaId) => {
                    dataForFormulate.grid[areaId] = saveData[areaId];
                })
                try {
                    ncProps.validateToSave( dataForFormulate , () => {
                        handle();
                    })
                } catch(e) {
                    console.error('该节点没配置验证公式');
                    handle();
                }

            } else {
                handle();
            }
        }
    }

}